<?php 
namespace app\index\model;
use think\Model;
use think\Session;
use think\Db;
use think\paginate;
class Allproblems_model extends Model{

	//首页 分页 搜索
	public function select($params)
    {
		$model = Db::table('problem')->order('problem_id desc');
		if (!empty($params['problem_project']))
		{
			$model->where('problem_project',$params['problem_project']);
		}
		if (!empty($params['problem_title']))
		{
			$model->where('problem_title',$params['problem_title']);
		}
		$data = $model->paginate(8,false,['query'=>$params]);
		return $data;
	}

	// 详情
	public function details($params)
	{
		$model = Db::table('problem')->where('problem_id',$params['problem_id'])->find();
		return $model;
	}

	//判断登陆人和测试人员是否一致
	public function deng()
	{
        $data = array(
    		'username' => session('username'), 
    		);
		return $data;
	}

	  //测试人员
    public function assignee()
    {
        $model = Db::table('user')->select();
        return $model;
    }

	//日志
	public function logs($params)
	{
		
		$logs = Db::table('logs')->order('logs_id desc')->where('problem_id',$params['problem_id']);
		if (!empty($params['logs_operator']))
		{
			$logs->where('logs_operator',$params['logs_operator']);
		}
		if (!empty($params['logs_operation_behavior']))
		{
			$logs->where('logs_operation_behavior',$params['logs_operation_behavior']);
		}
		$data = $logs->paginate(6,false,['query'=>$params]);
		return $data;
	}

	//日志删除
	public function del($params)
	{
		$model = Db::table('logs')->where('logs_id',$params['logs_id'])->delete();
		return $model;
	}

	//完成
	public function cparamsy_out($params)
	{
		$params['problem_status'] = '9';
		$arr['problem_status'] = $params['problem_status'];
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['logs_operator'] 			 = session('username');
		$data['problem_id'] 			 = $params['problem_id'];
        $data['logs_operation_behavior'] = '完成任务';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
		$arr = Db::table('logs')->insert($data);
		return $model;
	}




	//关闭
	public function closed($params)
	{
		$params['problem_status'] = '2';
		$arr['problem_status'] = $params['problem_status'];
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['logs_operator'] 			 = session('username');
		$data['problem_id'] 			 = $params['problem_id'];
        $data['logs_operation_behavior'] = '关闭任务';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
		$arr = Db::table('logs')->insert($data);

	}
	//已关闭 左侧边栏
	public function menu_closed($params)
	{
		$menu_closed = Db::query("select * from problem WHERE problem_status = 2");
		return $menu_closed;
	}




	//延期
	public function extension($params)
	{
		
		$arr['problem_planned_end_date'] = $params['problem_planned_end_date'];
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['problem_id'] 			 = $params['problem_id'];
		$data['logs_operator']			 = session('username');
        $data['logs_operation_behavior'] = '延期任务';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
		$arr = Db::table('logs')->insert($data);

	}
	//已延期 左侧边栏
	public function menu_extension($params)
	{
		$menu_extension = Db::query("select * from problem WHERE problem_status = 3");
		return $menu_extension;
	}

    //拒绝   按钮
	public function rejected($params)
	{
		$params['problem_status'] = '1';
		$arr['problem_status'] = $params['problem_status'];
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['logs_operator'] 			 = session('username');
		$data['problem_id'] 			 = $params['problem_id'];
        $data['logs_operation_behavior'] = '拒绝任务';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
		$arr = Db::table('logs')->insert($data);

	}

	
	//通过
	public function by($params)
	{
		$params['problem_status'] = '4';
		$arr['problem_status'] = $params['problem_status'];
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['logs_operator'] 			 = session('username');
		$data['problem_id'] 			 = $params['problem_id'];
        $data['logs_operation_behavior'] = '解决任务';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
		$arr = Db::table('logs')->insert($data);

	}
	//不通过
	public function fail($params)
	{
		$params['problem_status'] = '5';
		$arr['problem_status'] = $params['problem_status'];
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['logs_operator'] 			 = session('username');
		$data['problem_id'] 			 = $params['problem_id'];
        $data['logs_operation_behavior'] = '审核不通过';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
		$arr = Db::table('logs')->insert($data);

	}


	//未解决
	public function unsolved($params)
	{
		$params['problem_status'] = '6';
		$arr['problem_status'] = $params['problem_status'];
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['logs_operator'] 			 = session('username');
		$data['problem_id'] 			 = $params['problem_id'];
        $data['logs_operation_behavior'] = '打开了任务/任务未解决';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
		$arr = Db::table('logs')->insert($data);

	}

	//已解决 任务按钮
	public function solved($params)
	{
		$params['problem_status'] = '7';
		$arr['problem_status'] = $params['problem_status'];
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['logs_operator'] 			 = session('username');
		$data['problem_id'] 			 = $params['problem_id'];
        $data['logs_operation_behavior'] = '已解决任务';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
		$arr = Db::table('logs')->insert($data);

	}
	//已解决 左侧边栏
	public function menu_solved($params)
	{
		$menu_solved = Db::query("select * from problem WHERE problem_status = 7");
		return $menu_solved;
	}



	//审核
	public function review($params)
	{
		$params['problem_status'] = '8';
		$arr['problem_status'] = $params['problem_status'];
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['logs_operator'] 			 = session('username');
		$data['problem_id'] 			 = $params['problem_id'];
        $data['logs_operation_behavior'] = '已解决任务';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
		$arr = Db::table('logs')->insert($data);

	}
	//待审核
	public function pending($params)
	{
		$params['problem_status'] = '9';
		$arr['problem_status'] = $params['problem_status'];
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['logs_operator'] 		     = session('username');
		$data['problem_id'] 		     = $params['problem_id'];
        $data['logs_operation_behavior'] = '待审核任务';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
		$arr = Db::table('logs')->insert($data);

	}

	// 分配测试员
	public function problem_tester($params)
	{
		$params['problem_status'] = '11';
		$arr['problem_status'] = $params['problem_status']; //状态
		$arr['problem_tester'] = $params['problem_tester']; //测试员
		$arr['problem_test_replication'] = $params['problem_test_replication'];  //测试复现
		$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

		$data['problem_id'] 		     = $params['problem_id'];
		$data['logs_operator'] 		     = session('username');
		$data['problem_id'] 		     = $params['problem_id'];
        $data['logs_operation_behavior'] = '分配测试员';
        $data['logs_designated_person']  = $params['logs_designated_person'];
        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
        $data['logs_note']     			 = $params['logs_note'];
        $arr = Db::table('logs')->insert($data);

	}


	//接受任务 任务按钮
	public function accept_mission($params)
	{
		$params['problem_status'] = '6';
		
		$where = array (															
            'problem_planned_end_date'=>$params['problem_planned_end_date'],
        );
		$sql = Db::table('problem')->where($where)->find();
        if($sql) {	
	        $arr['problem_status'] 			 = $params['problem_status'];
			$arr['problem_planned_end_date'] = $params['problem_planned_end_date'];
			$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);

			$data['logs_operator'] 			 = session('username');
			$data['problem_id'] 			 = $params['problem_id'];
	        $data['logs_operation_behavior'] = '接受任务';
	        $data['logs_designated_person']  = $params['logs_designated_person'];
	        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
	        $data['logs_note']     			 = $params['logs_note'];
			$arr = Db::table('logs')->insert($data);
		}else{
			$arr['problem_status'] 			 = $params['problem_status'];
			$arr['problem_planned_end_date'] = $params['problem_planned_end_date'];
			$model = Db::table("problem")->where('problem_id',$params['problem_id'])->setField($arr);
			
			$data['logs_operator'] 			 = session('username');
			$data['problem_id'] 			 = $params['problem_id'];
	        $data['logs_operation_behavior'] = '接受任务,修改任务截至时间';
	        $data['logs_designated_person']  = $params['logs_designated_person'];
	        $data['logs_operation_time']     = date('Y-m-d H:i:s',time());
	        $data['logs_note']     			 = $params['logs_note'];
			$arr = Db::table('logs')->insert($data);
		}
	}
}

?>